home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / stdio / test-popen.c < prev    next >
C/C++ Source or Header  |  1994-05-18  |  1KB  |  68 lines

  1. #include <ansidecl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void
  6. DEFUN(write_data, (stream), FILE *stream)
  7. {
  8.   int i;
  9.   for (i=0; i<100; i++)
  10.     fprintf (stream, "%d\n", i);
  11.   if (ferror (stream))  {
  12.     fprintf (stderr, "Output to stream failed.\n");
  13.     exit (1);
  14.     }
  15. }
  16.  
  17. void
  18. DEFUN(read_data, (stream), FILE *stream)
  19. {
  20.   int i, j;
  21.  
  22.   for (i=0; i<100; i++)
  23.     {
  24.       if (fscanf (stream, "%d\n", &j) != 1 || j != i)
  25.     {
  26.       if (ferror (stream))
  27.         perror ("fscanf");
  28.       puts ("Test FAILED!");
  29.       exit (1);
  30.     }
  31.     }
  32. }
  33.  
  34. int
  35. DEFUN_VOID(main)
  36. {
  37.   FILE *output, *input;
  38.   int wstatus, rstatus;
  39.  
  40.   output = popen ("/bin/cat >tstpopen.tmp", "w");
  41.   if (output == NULL)
  42.     {
  43.       perror ("popen");
  44.       puts ("Test FAILED!");
  45.       exit (1);
  46.     }
  47.   write_data (output);
  48.   wstatus = pclose (output);
  49.   printf ("writing pclose returned %d\n", wstatus);
  50.   input = popen ("/bin/cat tstpopen.tmp", "r");
  51.   if (input == NULL)
  52.     {
  53.       perror ("tstpopen.tmp");
  54.       puts ("Test FAILED!");
  55.       exit (1);
  56.     }
  57.   read_data (input);
  58.   rstatus = pclose (input);
  59.   printf ("reading pclose returned %d\n", rstatus);
  60.  
  61.   puts (wstatus | rstatus  ? "Test FAILED!" : "Test succeeded.");
  62.   exit (wstatus | rstatus);
  63. }
  64.  
  65.   
  66.  
  67.   
  68.